import asyncio
from env_tools import get_env_value
from langchain_community.chat_models import ChatTongyi

# 获取apikey
api_key = get_env_value("QIANWEN_API_KEY")
if api_key is None:
    raise ValueError("请设置环境变量QIANWEN_API_KEY")

# 创建模型实例
model = ChatTongyi(
    model = "qwen3-max",
    temperature=0,
    max_tokens=None,
    timeout=None,
    api_key=api_key,
    max_retries=2,
)

async def main():
    questions = [
        "1+1等于几?",
        "Python与Java当前哪个语言更流行?",
        "你是哪家公司的模型?"
    ]
    # 异步调用
    # invoke()是同步的，需要异步的话需要使用ainvoke()
    tasks = [asyncio.create_task(model.ainvoke(q)) for q in questions]
    # 等待所有任务完成
    responses = await asyncio.gather(*tasks)
    for q, r in zip(questions, responses):
        print("问题:", q, "\n", r.content, "\n")

# 运行
asyncio.run(main())